package com.main;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ParseConfig {
	private static Logger logger=Logger.getLogger(ParseConfig.class.getName());

	public static ExtendMap<String,ExtendMap<String,String>> config=new ExtendMap<String,ExtendMap<String,String>>();
	
	public static ExtendMap<String,String> getConfigMap(String name){
		if(config.size() == 0){execute();}
		return config.get(name);
	}
	
	public static void execute(){
		try{
			DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			DocumentBuilder db=dbf.newDocumentBuilder();
			Document doc=db.parse(ParseConfig.class.getResource("/payConfig.xml").getPath());
			ExtendMap<String,String> database=new ExtendMap<String,String>();
			NodeList dbList=doc.getElementsByTagName("database");
			if(dbList!=null && dbList.getLength()>0){
				Element element=(Element)dbList.item(0);
				NodeList proList=element.getElementsByTagName("property");
				for(int j=0;j<proList.getLength();j++){
					Element proElement=(Element)proList.item(j);
					database.put(proElement.getAttribute("name"),proElement.getAttribute("value"));
				}
			}
			config.put("database",database);
			
			ExtendMap<String,String> notify=new ExtendMap<String,String>();
			NodeList notifyList=doc.getElementsByTagName("notify");
			if(notifyList!=null && notifyList.getLength()>0){
				Element element=(Element)notifyList.item(0);
				NodeList proList=element.getElementsByTagName("property");
				for(int j=0;j<proList.getLength();j++){
					Element proElement=(Element)proList.item(j);
					notify.put(proElement.getAttribute("name"),proElement.getAttribute("value"));
				}
			}
			config.put("notify",notify);
			
			ExtendMap<String,String> alipay=new ExtendMap<String,String>();
			NodeList alipayList=doc.getElementsByTagName("alipay");
			if(alipayList!=null && alipayList.getLength()>0){
				Element element=(Element)alipayList.item(0);
				NodeList proList=element.getElementsByTagName("property");
				for(int j=0;j<proList.getLength();j++){
					Element proElement=(Element)proList.item(j);
					alipay.put(proElement.getAttribute("name"),proElement.getAttribute("value"));
				}
			}
			config.put("alipay",alipay);

			//app类型
			ExtendMap<String,String> appType=new ExtendMap<String,String>();
			NodeList appTypeList = doc.getElementsByTagName("appType");
			if(appTypeList != null && appTypeList.getLength() > 0){
				Element element = (Element) appTypeList.item(0);
				NodeList proList=element.getElementsByTagName("property");
				for(int j=0;j<proList.getLength();j++){
					Element proElement=(Element)proList.item(j);
					appType.put(proElement.getAttribute("name"),proElement.getAttribute("value"));
				}
			}
			config.put("appType",appType);

			//微信不同端的appId是不一样的
			for (String typeName : config.get("appType").keySet()) {
				NodeList weixinPayList = doc.getElementsByTagName("weixin."+typeName);
				ExtendMap<String,String> aweixinPayType=new ExtendMap<String,String>();
				if(weixinPayList != null && weixinPayList.getLength() > 0){
					Element element = (Element) weixinPayList.item(0);
					NodeList proList=element.getElementsByTagName("property");
					for(int j=0;j<proList.getLength();j++){
						Element proElement=(Element)proList.item(j);
						aweixinPayType.put(proElement.getAttribute("name"),proElement.getAttribute("value"));
					}
				}
				config.put("weixin."+typeName,aweixinPayType);
			}

		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
	}
	
	public static void main(String args[]) throws Exception{
		execute();
		System.out.println(config);
	}

}